package com.sczmgk.hotline_pzh.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sczmgk.hotline_pzh.MyApp;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.base.UploadPicBaseActivity;
import com.sczmgk.hotline_pzh.callback.HttpFailCallback;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.AppealPicBean;
import com.sczmgk.hotline_pzh.entity.ComplainInfo;
import com.sczmgk.hotline_pzh.entity.ComplaintMoreInfoBean;
import com.sczmgk.hotline_pzh.entity.Picurls;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.utils.UIUtils;
import com.sczmgk.hotline_pzh.view.MyGridView;
import com.sczmgk.hotline_pzh.view.TextItemView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/29 0029.
 */
public class ComplaintDetailMoreInfomationActivity extends UploadPicBaseActivity {

    /*投诉处理界面与审核界面发送的消息*/
    protected static final int COMPLAINT_DETAIL = 100;

    /*投诉处理更多信息的封装*/
    protected ComplaintMoreInfoBean complaintMoreInfoBean;

    /*投诉更多界面的标题,事发时间,事发地址,投诉内容以及投诉编号和投诉时间*/
    protected TextItemView mTivTitle;
    protected TextItemView mTivDate;
    protected TextItemView mTivAddress;
    protected TextView mTvContent;
    protected TextItemView mTivNumber;
    protected TextItemView mTivComplaintDate;


    /*图片的容器和控件*/
    protected MyGridView mAppealGridView;
    protected LinearLayout mAppealGrideViewContainer;

    /*用户投诉上传的图片集合*/
    protected ArrayList<AppealPicBean> appealPics = new ArrayList<>();
    protected AppealAdapter appealAdapter;

    /*投诉审核的控件*/
    protected LinearLayout mLayoutInvest;

    protected Intent getIntent;

    /*投诉内容的文本是否显示所有*/
    protected TextView mShowAll;
    protected boolean isShowAll = false;
    protected ComplainInfo complaintInfo;
    private LinearLayout mImageItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_detail_more_info);

        getIntent = getIntent();

        setHeader();

        initView();

        //初始化数据
        initData();


    }

    /**
     * 设置头
     */
    public void setHeader() {
        setTitle(R.string.complaint_detail_header_title);
        setLeftBtnVisibility(View.VISIBLE);
    }

    /**
     * 初始化控件
     */
    @Override
    public void initView() {
          super.initView();
        //标题
        mTivTitle = (TextItemView) mLinearLayoutContent.findViewById(R.id.tiv_title);
        //事发时间
        mTivDate = (TextItemView) mLinearLayoutContent.findViewById(R.id.tiv_date);
        //事发地址
        mTivAddress = (TextItemView) mLinearLayoutContent.findViewById(R.id.tiv_address);
        //投诉内容
        mTvContent = (TextView) mLinearLayoutContent.findViewById(R.id.tv_complaint_detail_more_content);
        //投诉编号
        mTivNumber = (TextItemView) mLinearLayoutContent.findViewById(R.id.tiv_number);
        //投诉时间
        mTivComplaintDate = (TextItemView) mLinearLayoutContent.findViewById(R.id.tiv_complaint_date);

        //是否显示全部投诉内容
        mShowAll = (TextView) mLinearLayoutContent.findViewById(R.id.tv_complaint_more_info_show_all);

        //投诉详情相关图片的控件和容器
        mAppealGridView = (MyGridView) mLinearLayoutContent.findViewById(R.id.gv_appeal_pic);
        mAppealGrideViewContainer = (LinearLayout) mLinearLayoutContent.findViewById(R.id.ll_appeal_gridview_container);

        //投诉审核的布局
        mLayoutInvest = (LinearLayout) findViewById(R.id.ll_more_complaint_investigation);

        mImageItem = (LinearLayout) findViewById(R.id.ll_complaint_image_item);

        mLayoutInvest.setVisibility(View.GONE);
        //设置相关图片的Adapter
        appealAdapter = new AppealAdapter();
        mAppealGridView.setAdapter(appealAdapter);
    }

    /**
     * 初始化数据
     */
    public void initData() {

        //如果是投诉详情跳转的界面,通过传递的id查询网络投诉数据
        complaintInfo = (ComplainInfo) getIntent.getSerializableExtra(Constants.COMPLAIN_INFO_KEY);
        Map<String, Object> infos = new HashMap<>();
        infos.put("id", complaintInfo.id);
        infos.put("imei", MyApp.imei);


        if (loadingDialog != null) {
            DialogFactory.showDialog(loadingDialog);
        }

        MyHttp.requestByFailCallback(Constants.BASE_URL + Constants.GET_COMPLAINTS_DETAIL_URL, infos, MyHttp.GET, this, false, null, new SimplePaserJsonCallback() {
            @Override
            public void onVerifyIsNull() {
                DialogFactory.dismissLoadingDialog(loadingDialog);
            }

            @Override
            public void onVerifySuccess(String dataRaw, boolean isCache) {

                DialogFactory.dismissLoadingDialog(loadingDialog);

                try {
                    paserInfoJson(dataRaw, isCache);
                } catch (Exception e) {
                    MyLog.e(TAG, "解析json错误");
                    e.printStackTrace();
                }
            }
        }, new HttpFailCallback() {
            @Override
            public void onServiceErrorCallback() {
//                MyToast.show(ComplaintDetailMoreInfomationActivity.this, "您的网络有问题,请检查您的网络");
                DialogFactory.dismissLoadingDialog(loadingDialog);
            }
        });

    }

    /**
     * 处理投诉详情的Json数据
     *
     * @param dataRaw
     * @param isCache
     */
    private void paserInfoJson(String dataRaw, boolean isCache) {

        if (Tools.isEmpty(dataRaw)) {
            MyLog.i("获取投诉详情数据", "get complaint detail json error");
            return;
        }

        //解析投诉详情的Json数据并存入JavaBean中
        Gson gson = new Gson();
        complaintMoreInfoBean = gson.fromJson(dataRaw, ComplaintMoreInfoBean.class);

        //发送消息更新ui
        mHandler.sendEmptyMessage(COMPLAINT_DETAIL);
    }


    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
            case COMPLAINT_DETAIL:
                //更改投诉详情ui
                refreshUi();
                break;
        }
    }

    /**
     * 刷新投诉详情ui
     */
    public void refreshUi() {

        //设置标题,事发时间,事发地址,投诉内容,投诉编号,投诉时间
        mTivTitle.setText(complaintMoreInfoBean.title);
        mTivDate.setText(complaintMoreInfoBean.operationtime);
        if (!Tools.isEmpty(complaintMoreInfoBean.area)) {
            mTivAddress.setText(complaintMoreInfoBean.area + complaintMoreInfoBean.address);
        }else{
            mTivAddress.setText(complaintMoreInfoBean.address);
        }

        //将文本中的<br/>标签转换为\n
        String content = complaintMoreInfoBean.content;
        String replaceContent = Tools.replaceBr(content);

        mTvContent.setText(replaceContent);
        mTivNumber.setText(complaintMoreInfoBean.id);
        mTivComplaintDate.setText(complaintMoreInfoBean.createtime);

        //设置显示全部的按钮
        final int lineCount = mTvContent.getLineCount();
        if (lineCount <= 4) {
            mShowAll.setVisibility(View.GONE);
        } else {
            mTvContent.setMaxLines(4);
            mTvContent.setEllipsize(TextUtils.TruncateAt.END);
            mShowAll.setVisibility(View.VISIBLE);
            isShowAll = true;
        }

        //显示全部按钮的点击事件
        mShowAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isShowAll) {
                    mShowAll.setText(R.string.show_all);
                    mTvContent.setMaxLines(4);
                    mTvContent.setEllipsize(TextUtils.TruncateAt.END);
                    isShowAll = false;
                } else {
                    mTvContent.setMaxLines(lineCount);
                    mShowAll.setText(R.string.close_show_all);
                    isShowAll = true;
                }
            }
        });

        //设置图片
        List<Picurls> picurls = complaintMoreInfoBean.picurls;
        if (picurls != null && picurls.size() > 0) {
            appealPics = getAppealList(picurls);
            appealAdapter.setPicList(appealPics);
            appealAdapter.notifyDataSetChanged();
        } else {
            mImageItem.setVisibility(View.GONE);
        }
    }

    public ArrayList<AppealPicBean> getAppealList(List<Picurls> picurls) {
        ArrayList<AppealPicBean> lists = new ArrayList<>();
        for (int i = 0; i < picurls.size(); i++) {
            AppealPicBean bean = new AppealPicBean();
            bean.setImageUri(picurls.get(i).smallpic);
            lists.add(bean);
        }
        return lists;
    }

    /**
     * 申诉图片adapter
     */
    public class AppealAdapter extends BaseAdapter {

        private ArrayList<AppealPicBean> picsInfos;

        public void setPicList(ArrayList<AppealPicBean> pics) {
            picsInfos = pics;
        }

        @Override
        public int getCount() {
            return picsInfos == null ? 0 : picsInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View view, ViewGroup viewGroup) {

            view = View.inflate(ComplaintDetailMoreInfomationActivity.this, R.layout.appeal_pic_item, null);
            ImageView imageView = (ImageView) view.findViewById(R.id.iv_appeal_pic);
            bitmapUtils.display(imageView, picsInfos.get(position).getImageUri());
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    UIUtils.startActivity(ComplaintDetailMoreInfomationActivity.this, AppealLargePicActivity.class, "position", position, "picUrls", picsInfos);
                }
            });
            return view;
        }

    }

    @Override
    public boolean getIsNeedLogin() {
        return true;
    }

}
